package utstat.listeners;

import static utstat.Type.kill;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import utstat.Team;
import utstat.UtEvent;
import utstat.UtEventListener;

/**
 3 kills of enemies < 10 seconds old
 */
public class SpawncampListener implements UtEventListener {

	private Map<String, Integer> spawnKills = new HashMap<String, Integer>();
	private Map<String, Integer> spawnTimes = new HashMap<String, Integer>();
	
	private int currentRound = -1;

	@Override
	public String getIntestingFact(String player) {
		return null;
	}

	@Override
	public void processEvent(UtEvent e) {
		if (e.getRound() != currentRound) {
			nextRound();
			currentRound = e.getRound();
		}
		if (e.getType() == kill) {
			if(e.getSrc()==null) return;
			Integer enemySpawnTime = spawnTimes.get(e.getDest());
			if(enemySpawnTime!=null) {
			int enemyAge = e.getTime()-enemySpawnTime;
			if(enemyAge<15) increment(e.getSrc(), spawnKills);

			Integer kills = spawnKills.get(e.getSrc());
			if(kills!=null&&kills==3) { System.out.println(e.getSrc()+" had his tent up and is roasting weenies!");
			increment(e.getSrc(), spawnKills); // so it won't be 3 next time
			}
			}
			spawnTimes.put(e.getDest(), e.getTime());
			spawnKills.put(e.getDest(), 0);
			}
		}

	
	private int increment(String player, Map<String, Integer> countMap) {
		Integer count = countMap.get(player);
		if (count == null) count = 0;
		count++;
		countMap.put(player, count);
		return count;
	}

	private void nextRound() {
		System.out.println("------");

		spawnKills = new HashMap<String,Integer>();
		spawnTimes = new HashMap<String,Integer>();
	}

}
